<template>
  <div>
    <el-table :data="filterTableData" style="width: 100%">
      <el-table-column label="Name" prop="name" />
      <el-table-column label="User_id" prop="user_id" />
      <el-table-column label="Email" prop="email" />
      <el-table-column label="Phone" prop="phone" />
      <el-table-column label="Date" prop="add_time" />
      <el-table-column
          label="Tag"
          prop="tag"
          width="100"
          :filters="[
        { text: 'Home', value: 'Home' },
        { text: 'Office', value: 'Office' },
      ]"
          :filter-method="filterTag"
          filter-placement="bottom-end"
      >
        <template #default="scope">
          <el-tag
              :type="scope.row?.tag === 'Home' ? 'primary' : 'success'"
              disable-transitions
          >{{ scope.row?.tag }}</el-tag
          >
        </template>
      </el-table-column>

      <el-table-column align="right">
        <template #header>
          <el-input v-model="search" size="small" placeholder="Type to search" />
        </template>

        <template #default="scope">
          <el-button size="small" type="success" @click="handleEdit(scope.$index, scope.row), dialogFormVisible = true">
            Edit
          </el-button>
          <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
          >
            Delete
          </el-button>
        </template>
      </el-table-column>

    </el-table>
    <el-button class="mt-4" style="width: 100%; margin-top: 20px" @click="handleCreate">
      Create User
    </el-button>

    <el-dialog v-model="dialogFormVisible" :title="dialogTitle" width="400">
      <el-form :model="editUserInfo">
        <el-form-item label="Nmae" label-width="100px">
          <el-input v-model="editUserInfo.name"  autocomplete="off"/>
        </el-form-item>
        <el-form-item label="Email" label-width="100px">
          <el-input  v-model="editUserInfo.email" autocomplete="off" />
        </el-form-item>
        <el-form-item label="Phone" label-width="100px">
          <el-input v-model="editUserInfo.phone"  autocomplete="off"/>
        </el-form-item>
        <el-form-item label="Tag" label-width="100px">
          <el-select v-model="editUserInfo.tag" placeholder="Please select"  autocomplete="off">
            <el-option label="admin" value="admin"/>
            <el-option label="user" value="user"/>
          </el-select>
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">Cancel</el-button>
          <el-button type="primary" @click="modify_User_Information">
            Confirm
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { computed, ref } from 'vue'
import {Edit_Userinfo,Register} from "~/utils/api";
import {ElMessage} from "element-plus";


interface User {
  date: string
  name: string
  user_id: number
  email: string
  phone: number
  address: string
  tag: string
}

const search = ref()
const userData: any = ref([])
const dialogFormVisible = ref(false)
const editUserInfo = ref('')
const dialogTitle = ref('')

const tableData = computed(() => {
  return userData.value.map( (users: any) => {
    return users
  })
})


const filterTableData = computed(() =>
    tableData.value.filter(
        (data: { name: string; }) =>
            !search.value ||
            data.name.toLowerCase().includes(search.value.toLowerCase())
    )
)



const filterTag = (value: string, row: User) => {
  return row?.tag === value
}
const handleEdit = (index: number, row: User) => {
  dialogTitle.value = "Edit"
  editUserInfo.value = row
}

const handleCreate = async() => {
  editUserInfo.value = ''
  dialogFormVisible.value = true
  dialogTitle.value = "Create"
  if (editUserInfo.value){
    await Register(editUserInfo).then(res => {
      console.log(res)
    })
  }
}

const modify_User_Information = () => {
      Edit_Userinfo(editUserInfo).then( res => {
      console.log(res)
      dialogFormVisible.value = false
      ElMessage({
        message: "User updated successfully",
        type: 'success',
        duration: 1000,
      })
    })
}
const handleDelete = (index: number, row: User) => {
  console.log(index, row)
}

const get_userinfo = async () => {
    await $fetch("https://service.starbucket.cn/service/user/userinfo", {
      method: 'GET',
    }).then( async res => {

      res["userinfo"].forEach((user: { name: string }) => {
        userData.value.push(user)
      })
      console.log(userData)
    })
}




onMounted(async () => {
    await get_userinfo()
})

</script>